/*
 * Home.java
 *
 * Created on January 29, 2007, 11:11 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ge.info.pages;

import ge.info.domain.ListsProvider;
import ge.info.domain.MainResultDetail;
import ge.info.domain.Organization;
import ge.ktapestry.pages.BasePageExt;
import ge.ktools.StringUtils;
import org.apache.hivemind.util.PropertyUtils;
import org.apache.tapestry.IPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InitialValue;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;

public abstract class OrgResultInfo extends BasePageExt implements PageBeginRenderListener
{

    @InjectObject("spring:listsProvider")
    public abstract ListsProvider getListsProvider();

    @Persist
    public abstract Organization getOrganization();

    public abstract void setOrganization(Organization organization);

    @InitialValue("null")
    public abstract Integer getOrganizationID();

    public abstract void setOrganizationID(Integer organizationID);

    @Persist
    public abstract Integer getOid();

    public abstract void setOid(Integer oid);


    public abstract MainResultDetail getDetail();

    public abstract void setDetail(MainResultDetail mainResultDetail);
   

    public static int SERVICE_ID = -3;

    public void pageBeginRender(PageEvent event)
    {

        if (!event.getRequestCycle().isRewinding())
        {
             setOid(getOrganizationID());

            if (getOrganization() == null)
            {
                setOrganization(getDbExecutor().getOrganizationByID(getOrganizationID()));
            }
           
        }
        return;
    }

   
     public void openMainResultPage(IRequestCycle cycle, Integer mainID)
    {

        IPage page = cycle.getPage("OrgResultPage");
        PropertyUtils.write(page, "organizationID", mainID);
        PropertyUtils.write(page, "organization", null);
        cycle.activate(page);
    }

      public void opensearchStreetdet(IRequestCycle cycle, Integer streetID)
    {

       IPage page = cycle.getPage("SearchStreetdet");
        PropertyUtils.write(page, "streetID", streetID);
        PropertyUtils.write(page, "street", null);
        cycle.activate(page);
    }

    public void sendSMS(IRequestCycle cycle, Organization organization)
    {

        Organization org = getOrganization();
        StringBuilder sb = new StringBuilder();

        sb.append(org.getName()).append(" ");


        if (org.getDirector() != null)
        {
            sb.append(org.getDirector()).append("; ");
        }

        if (org.getWebaddress() != null)
        {
            sb.append(org.getWebaddress()).append("; ");
        }

        if (org.getMail() != null)
        {
            sb.append(org.getMail()).append("; ");
        }


        if (org.getWorkingHours() != null)
        {
            sb.append(org.getWorkingHours()).append(" ");
        }
        

        getDbExecutor().insertSms(getSession(), sb.toString(), SERVICE_ID);
    }
   
}
